package com.aidex.web.controller.api;

import com.aidex.biz.domain.ProductInfo;
import com.aidex.biz.domain.ProductSku;
import com.aidex.biz.domain.RubbishType;
import com.aidex.biz.service.ProductInfoService;
import com.aidex.biz.service.ProductSkuService;
import com.aidex.biz.service.RubbishTypeService;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.utils.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 商家商品管理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-21
 */
@RestController("StoreGoodsController")
@RequestMapping("/api/goods")
@Api(tags = "商家商品管理")
@Component
public class StoreGoodsController extends BaseController {



    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductSkuService productSkuService;
    @Autowired
    private RubbishTypeService rubbishTypeService;


    /**
     * 添加商品
     * @param productInfo
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ProductInfo productInfo) {
        boolean proIsOk=false,proSkuOk=false;
        RubbishType rubbishType=rubbishTypeService.get(productInfo.getRubbishTypeId());
        productInfo.setRubbishTypeName(rubbishType.getDeptName());
        if(StringUtils.isEmpty(productInfo.getId())){
//            RubbishType rubbishType=rubbishTypeService.get(productInfo.getRubbishTypeId());
//            productInfo.setRubbishTypeName(rubbishType.getDeptName());
            productInfo.setId(UUID.randomUUID().toString());
            productInfo.setIsNewRecord();
             proIsOk = productInfoService.save(productInfo);
            List<ProductSku> skus = productInfo.getProductSkuList();
             proSkuOk = false;
            for (int i = 0; i < skus.size(); i++) {
                ProductSku sku = skus.get(i);
                sku.setProductId(productInfo.getId());
                sku.setStatus("0");
                sku.setSort(i);
                proSkuOk = productSkuService.save(sku);
            }
        }else{
            proIsOk = productInfoService.save(productInfo);
            List<ProductSku> skus = productInfo.getProductSkuList();
            proSkuOk = false;
            for (int i = 0; i < skus.size(); i++) {
                ProductSku sku = skus.get(i);
                sku.setProductId(productInfo.getId());
                sku.setStatus("0");
                sku.setSort(i);
                proSkuOk = productSkuService.save(sku);
            }
        }

        return R.status(proIsOk && proSkuOk);
    }


    @GetMapping("/deleteGoods")
    public R deleteGoods(@RequestParam String[] ids) {
        return R.status(productInfoService.deleteProductInfoByIds(ids));
    }

    @GetMapping("/getGoods")
    public R getGoods(@RequestParam String id) {
        return R.data(productInfoService.get(id));
    }





}